package com.longcai.piaojia.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.longcai.piaojia.R;
import com.longcai.piaojia.util.MyToast;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

/**
 * Created by Administrator on 2017/1/11.
 */
public abstract class EvaluateDialog extends Dialog {

    private EditText dialog_evaluate_et;
    private RelativeLayout dialog_evaluate_sure_rl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) findViewById(R.id.dialog_evaluate_ll));

        getWindow().setGravity(Gravity.BOTTOM);

    }

    public EvaluateDialog(final Context context) {
        super(context, R.style.MyDialog);

        setContentView(R.layout.dialog_evaluate);

        dialog_evaluate_et = (EditText) findViewById(R.id.dialog_evaluate_et);
        dialog_evaluate_sure_rl = (RelativeLayout) findViewById(R.id.dialog_evaluate_sure_rl);


        dialog_evaluate_et.addTextChangedListener(new TextWatcher() {

            private CharSequence temp = "";
            private int maxLength = 50;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = dialog_evaluate_et.getText();

                //不让输入空格的方法
                if (s.toString().contains(" ")) {
                    String[] str = s.toString().split(" ");
                    String str1 = "";
                    for (int i = 0; i < str.length; i++) {
                        str1 += str[i];
                    }
                    dialog_evaluate_et.setText(str1);

                    dialog_evaluate_et.setSelection(start);

                }

                int len = editable.length();

                if (len > maxLength) {
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, maxLength);
                    dialog_evaluate_et.setText(newStr);
                    editable = dialog_evaluate_et.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

                if (temp.length() > maxLength) {
                    MyToast.show(context, "最多输入" + maxLength + "位", MyToast.LENGTH_SHORT);
                }
            }
        });

        dialog_evaluate_et.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER) {

                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

                    if (imm.isActive()) {

                        imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);

                    }

                    String content = dialog_evaluate_et.getText().toString();

                    onConfirm(content);

                    dismiss();

                    return true;

                }

                return false;
            }

        });

        dialog_evaluate_sure_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (v.getId()) {

                    case R.id.dialog_evaluate_sure_rl:

                        String content = dialog_evaluate_et.getText().toString();

                        onConfirm(content);

                        break;
                }

                dismiss();
            }
        });

        show();

    }

    protected abstract void onConfirm (String content);

}
